package com.shf.controller;


import com.github.pagehelper.PageInfo;
import com.shf.entity.Product;
import com.shf.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 叶枫
 * 控制层
 */
@Controller
@RequestMapping("/admin/")
public class ProductController {

    @Autowired
    @Qualifier("ps")
    private ProductService productService;


    /**
     * 添加产品
     */
    @RequestMapping("insertProduct")
    public String addProduct(Product product){

        //生成随机id
        product.setId(UUID.randomUUID().toString());
        //调用业务层方法
        productService.addProduct(product);



        //响应
        return "forward:/admin/product/findAllProducts.do?page=1&size=5";
    }

    /**
     * 检查产品编号是否存在
     */
    @RequestMapping("getProductNum")
    @ResponseBody
    public int getProductNum(String productNum){
      boolean bl =  productService.selectProductByNum(productNum);

      if (bl == true){
          return 1;
      }
      return 0;
    }


    @RequestMapping("product/findAllProducts")
    public ModelAndView showAllProducts(@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        ModelAndView mv = new ModelAndView();
        //调用业务层方法
        PageInfo<Product> pageInfo = productService.showAllProducts(page,size);
        //设置共享属性
        mv.addObject("pageInfo",pageInfo);

        //响应视图页面
        mv.setViewName("admin/product-list");
        return mv;

    }

    /**
     * 查询要修改的产品显示到form表单中
     * @param id
     * @return
     */
    @RequestMapping("product/editById")
    public ModelAndView editById( @RequestParam(name = "id") String id,ModelAndView mv){

       Product product = productService.editById(id);

       //共享属性
        mv.addObject("product",product);
        //响应
        mv.setViewName("admin/product-edit");
        return mv;

    }

    /**
     * 修改产品
     * @param product
     * @return
     */
    @RequestMapping("product/edit")
    public String edit (Product product) throws ParseException {
        //调用业务层方法
        productService.editProduct(product);

        //响应
        return "redirect:/admin/product/findAllProducts.do?page=1&size=5";

    }

    /**
     * 根据id删除产品
     * @param id
     * @param mv
     * @return
     */
    @RequestMapping("product/deleteById")
    public String deletePrductById(@RequestParam(name = "id") String id,ModelAndView mv){

        //调用业务层方法
        productService.deletePrductById(id);

        //响应
        return "redirect:/admin/product/findAllProducts.do?page=1&size=5";
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */

    @RequestMapping("product/deleteManny")
    @ResponseBody
    public String deleteManyByIds(String[] ids ){

        //调用Service层
        productService.deleteManyByIds(ids);

        return "1";
    }

    /**
     *
     * @return
     */
    @RequestMapping("product/showProductDetail")
    public ModelAndView showProductDetail(@RequestParam("id") String id,ModelAndView mv){

      Product product =   productService.showProductDetail(id);

        //共享属性
        mv.addObject("product",product);
        //响应
        mv.setViewName("admin/product-show");
        return mv;
    }

    /**
     *
     */
    @RequestMapping("product/product-add.do")
    public String addProduct(){
        return "admin/product-add";
    }
}
